package com.hu.zhcc.shiro.service.xsf.impl;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.common.entity.PageRequest;
import com.hu.zhcc.shiro.dao.xsf.TSportsItemLibraryMapper;
import com.hu.zhcc.shiro.entity.xsf.dto.THealthPlanDTO;
import com.hu.zhcc.shiro.entity.xsf.dto.TSportsItemLibraryDTO;
import com.hu.zhcc.shiro.entity.xsf.pojo.THealthPlan;
import com.hu.zhcc.shiro.entity.xsf.pojo.TSportsItemLibrary;
import com.hu.zhcc.shiro.service.xsf.TSportsItemLibraryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Transactional
@Service
public class TSportsItemLibraryImpl implements TSportsItemLibraryService {
    @Autowired
    private TSportsItemLibraryMapper tSportsItemLibraryMapper;

    @Override
    public Page<TSportsItemLibraryDTO> queryAllTSportsItemLibrary(Map<String, Object> paremeters, int offset, int limit) {
        int total = this.tSportsItemLibraryMapper.countTSportsItemLibrary(paremeters);
        List<TSportsItemLibraryDTO> dtoList = null;
        if(total > 0) {
            PageRequest pageRequest = new PageRequest(offset, limit, paremeters, null);
            List<TSportsItemLibrary> list = this.tSportsItemLibraryMapper.queryAllTSportsItemLibrary(pageRequest);
            dtoList = new ArrayList<TSportsItemLibraryDTO>(list.size());
            for(TSportsItemLibrary tSportsItemLibrary : list) {
                TSportsItemLibraryDTO dto = new TSportsItemLibraryDTO();
                BeanUtils.copyProperties(tSportsItemLibrary, dto);
                dtoList.add(dto);
            }
        }
        return new Page<TSportsItemLibraryDTO>(total, dtoList);
    }

    @Override
    public TSportsItemLibraryDTO getById(int id) {
        TSportsItemLibrary tSportsItemLibrary = this.tSportsItemLibraryMapper.selectByPrimaryKey(id);
        if(tSportsItemLibrary == null) {
            return null;
        }
        TSportsItemLibraryDTO dto = new TSportsItemLibraryDTO();
        BeanUtils.copyProperties(tSportsItemLibrary, dto);
        return dto;
    }

    @Override
    public TSportsItemLibraryDTO saveTSportsItemLibrary(TSportsItemLibraryDTO dto) {
        TSportsItemLibrary tSportsItemLibrary = new TSportsItemLibrary();
        BeanUtils.copyProperties(dto, tSportsItemLibrary);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        tSportsItemLibrary.setCreateTime(simpleDateFormat.format(new Date()));
        this.tSportsItemLibraryMapper.insert(tSportsItemLibrary);
        dto.setId(tSportsItemLibrary.getId());
        return dto;
    }

    @Override
    public int removeTSportsItemLibrary(int id) {
        return tSportsItemLibraryMapper.deleteByPrimaryKey(id);

    }

    @Override
    public int updateTSportsItemLibrary(TSportsItemLibrary tSportsItemLibrary) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        tSportsItemLibrary.setUpdateTime(simpleDateFormat.format(new Date()));
        return tSportsItemLibraryMapper.updateByPrimaryKeySelective(tSportsItemLibrary);

    }
}
